//
//  SMHDataPersonInfoView.swift
//  SMHCommerce
//
//  Created by lvzhao on 2020/6/2.
//  Copyright © 2020 lvzhao. All rights reserved.
//

import UIKit
import ReactiveCocoa

class SMHDataPersonInfoView: LZBaseView {

    var viewModel = SMHDataViewModel()
    override init(viewModel: LZBaseViewModel) {
        super.init(viewModel: viewModel)
        self.viewModel = viewModel as! SMHDataViewModel
        self.backgroundColor = UIColor.white
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK:_setupUI
    func setupUI()  {
        
        //MARK:个人信息
        let infoLab = UILabel()
        infoLab.text = "个人信息"
        infoLab.textColor = UIColorFromHex(rgbValue: 0x363636)
        infoLab.font = ktextBoldFont(size: 15)
        self.addSubview(infoLab)
        
        infoLab.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.height.equalTo(50)
            make.width.equalTo(120)
            make.top.equalTo(0)
        }
        
        
        let infoLineView = UIView()
        infoLineView.backgroundColor = UIColorFromHex(rgbValue: 0xE5E5E5)
        self.addSubview(infoLineView)
        infoLineView.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.height.equalTo(0.8)
            make.width.equalTo(kScreenWidth-30)
            make.top.equalTo(49)
        }
        
        
        //权限
        let levelLab = UILabel()
        levelLab.text = "权限:"
        levelLab.textColor = UIColorFromHex(rgbValue: 0xFF661B)
        levelLab.font = ktextBoldFont(size: 15)
        self.addSubview(levelLab)
        levelLab.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.height.equalTo(50)
            make.width.equalTo(120)
            make.top.equalTo(infoLineView.snp.bottom)
        }
        
        let levelNum = "权限: Lv3"
        let levelAttributedString = NSMutableAttributedString.init(string: levelNum)
        let levelDic = [NSAttributedString.Key.font: ktextFont(size: 15),
                        NSAttributedString.Key.foregroundColor:UIColorFromHex(rgbValue: 0x363636)]
        
        
        
        let range = NSRange(location: 0, length: "权限:".count)
        levelAttributedString.addAttributes(levelDic, range: range)
        levelLab.attributedText = levelAttributedString
        
        
        //待解锁
        let lockBtn = UIButton()
        lockBtn.setTitle("待解锁", for: .normal)
        lockBtn.setTitle("可解锁", for: .selected)
        lockBtn.titleLabel?.font = ktextFont(size: 12)
                
        if let selectImage = UIImage(gradientColors: [UIColorFromHex(rgbValue: 0xFFA61B),
                                                      UIColorFromHex(rgbValue: 0xFF661B)],
                               size: CGSize(width: 80, height: 28)) {
            lockBtn.setBackgroundImage(selectImage, for: .selected)
        }

        if let norImage = UIImage(color: UIColorFromHex(rgbValue: 0xD8D8D8),
                           size: CGSize(width: 80, height: 28)) {
            lockBtn.setBackgroundImage(norImage, for: .normal)

        }

        lockBtn.setTitleColor(UIColor.white, for: .normal)
        self.addSubview(lockBtn)
        lockBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-15)
            make.height.equalTo(28)
            make.width.equalTo(80)
            make.centerY.equalTo(levelLab.snp.centerY)
        }
        lockBtn.clipRectCorner(direction: .allCorners, cornerRadius: 14)
        lockBtn.isSelected = true
    
         // 监听按钮的点击事件
        lockBtn.reactive.controlEvents(.touchUpInside).observeValues { (button) in
            
            print("lockBtn")
            button.isSelected = !button.isSelected
        }

        
        
        let levelLineView = UIView()
        levelLineView.backgroundColor = UIColorFromHex(rgbValue: 0xE5E5E5)
        self.addSubview(levelLineView)
        levelLineView.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.height.equalTo(0.8)
            make.width.equalTo(kScreenWidth-30)
            make.top.equalTo(levelLab.snp.bottom).offset(-1)
        }
        
        //解锁订单
        let upgradeOrderLabel = UILabel()
        upgradeOrderLabel.text = "解锁订单"
        upgradeOrderLabel.textColor = UIColorFromHex(rgbValue: 0x363636)
        upgradeOrderLabel.font = ktextFont(size: 15)
        self.addSubview(upgradeOrderLabel)
              
        upgradeOrderLabel.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.height.equalTo(50)
            make.width.equalTo(120)
            make.top.equalTo(levelLineView.snp.bottom)
        }
              
        let statusName :[String] = ["全部","待付款","待发货","已发货","已完成"];
        let statusImageName :[String] = ["icon_order_all","icon_order_payment","icon_order_shipped_not","icon_order_shipped","icon_order_complete"]

        
        let witdth :Int = Int(kScreenWidth)/statusName.count
        var badgeLabs = Array<UILabel>()
        for (index,item) in statusName.enumerated(){
       
            let statusBtn = UIButton()
            statusBtn.setTitle(item, for: .normal)
            statusBtn.setTitleColor(UIColorFromHex(rgbValue: 0x868686), for: .normal)
            statusBtn.titleLabel?.font = ktextFont(size: 12)
            statusBtn.setImage(UIImage.init(named: statusImageName[index]), for: .normal)
            statusBtn.tag = 100 + index
            self.addSubview(statusBtn)
            statusBtn.snp.makeConstraints { (make) in
                make.left.equalTo(index * witdth)
                make.top.equalTo(upgradeOrderLabel.snp.bottom)
                make.width.equalTo(witdth)
                make.height.equalTo(45)
            }
            statusBtn.layoutButtonEdgeInsets(style: .ButtonEdgeInsetsStyleTop, space: 6)
            
            let badgeLab = UILabel()
            badgeLab.backgroundColor = UIColorFromHex(rgbValue: 0xFF661B)
            self.addSubview(badgeLab)
            badgeLab.snp.makeConstraints { (make) in
                make.left.equalTo(statusBtn.snp.centerX).offset(8)
                make.top.equalTo(statusBtn.snp.top).offset(-3)
                make.height.equalTo(10);
                make.width.equalTo(10);
            }
            badgeLab.clipRectCorner(direction: .allCorners, cornerRadius: 5)
            badgeLabs.append(badgeLab)
        }
    }
    
    
    
}
